﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DHLPricing.OrderServices;

namespace DHLPricing.OrderInfo
{
    public partial class OrderAddOrUpdate : Form
    {
        public Orders CurrentOrder = new Orders();

        public OrderAddOrUpdate()
        {
            InitializeComponent();
        }

        public void BindEditData()
        {
            txtCustomerCode.Text = CurrentOrder.CustomerCode;
            txtChannelCode.Text = CurrentOrder.ChannelCode;
            txtOrderNumberRef.Text = CurrentOrder.OrderNumberRef;
            txtOrderNumber.Text = CurrentOrder.OrderNumber;
            txtRealWeight.Text = CurrentOrder.RealWeight.ToString();
            txtVolumeWeight.Text = CurrentOrder.VolumeWeight.ToString();
            txtItemType.Text = CurrentOrder.ItemType == 0 ? "DOC" : "其它";
            txtPieces.Text = CurrentOrder.Pieces.ToString();
            txtCountryCN.Text = CurrentOrder.CountryCN;
            txtAddress.Text = CurrentOrder.Address;
            txtNotes.Text = CurrentOrder.Notes;

            txtBatchInfo.Text = CurrentOrder.BatchInfo;
            txtFlightInfo.Text = CurrentOrder.FlightInfo;
            dateTimePickerFlightTime.Value = CurrentOrder.FlightTime.HasValue ? CurrentOrder.FlightTime.Value : DateTime.MinValue;

            this.Text = string.Format("订单：{0}", CurrentOrder.ID);
            btnOK.Text = "修改";
        }

        private void GetCurrentOrderData()
        {
            CurrentOrder.CustomerCode = txtCustomerCode.Text;
            CurrentOrder.ChannelCode = txtChannelCode.Text;
            CurrentOrder.OrderNumberRef = txtOrderNumberRef.Text;
            CurrentOrder.OrderNumber = txtOrderNumber.Text;
            CurrentOrder.RealWeight = Convert.ToDouble(txtRealWeight.Text);
            if (!string.IsNullOrEmpty(txtVolumeWeight.Text))
            {
                CurrentOrder.VolumeWeight = Convert.ToDouble(txtVolumeWeight.Text);
            }
            CurrentOrder.ItemType = txtItemType.Text == "DOC" ? 0 : 1;
            CurrentOrder.Pieces = Convert.ToInt32(txtPieces.Text);
            CurrentOrder.CountryCN = txtCountryCN.Text;
            CurrentOrder.Address = txtAddress.Text;
            CurrentOrder.Notes = txtNotes.Text;

            CurrentOrder.FlightInfo = txtFlightInfo.Text;
            CurrentOrder.FlightTime = Convert.ToDateTime(dateTimePickerFlightTime.Text);
            CurrentOrder.BatchInfo = txtBatchInfo.Text;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            OrderServices.OrderServiceSoapClient client = new OrderServiceSoapClient();
            GetCurrentOrderData();
            string result = string.Empty;
            try
            {
                if (btnOK.Text == "修改")
                {
                    result = client.EditOrder(CurrentOrder);
                }
                else
                    result = client.AddOrder(CurrentOrder);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("操作失败，失败原因：" + ex.Message);
                client.Close();
                return;
            }

            if (result == "0")
            {
                MessageBox.Show("操作成功。");
                ((OrderInfo.OrdersManage)this.Owner).BindOrderData();
                this.Close();
            }
            else
                MessageBox.Show("操作失败，请稍后再试。");
        }
        
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
